package com.zyl.Ecs.common.domain.device.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.zyl.Ecs.common.domain.device.ext.SensorEcl;
import com.zyl.Ecs.common.utils.ObjectUtil;
import com.zyl.Ecs.common.domain.device.ext.PositionExt;
import com.zyl.Ecs.common.domain.device.ext.RoomExt;
import com.zyl.Ecs.common.domain.device.ext.SensorExt;
import lombok.Builder;
import lombok.Data;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.device.domain.vo
 * @ClassName: PositionVO
 * @Description TODO
 * @Author 翟玉龙
 * @Date 2024/12/5-9:35
 */
@Data
@Builder
public class PositionVO {

    @JsonProperty(value = "id")
    private Integer id;

    @JsonProperty(value = "name")
    private String name;

    @JsonProperty(value = "roomId")
    private Integer roomId;

    @JsonProperty(value = "roomName")
    private String roomName;

    @JsonProperty(value = "sensorNames")
    private List<String> sensorNames;

    @JsonProperty(value = "sensorIds")
    private List<Integer> sensorIds;

    @JsonProperty(value = "sensors")
    private List<SensorEcl> sensors;

    public static PositionVO toVO(PositionExt positionExt) {
        if (ObjectUtil.isNull(positionExt)) {
            return null;
        }
        return PositionVO.builder()
                .id(positionExt.getId())
                .name(positionExt.getName())
                .roomId(positionExt.getRoom().getId())
                .roomName(Optional.ofNullable(positionExt.getRoom())
                        .map(RoomExt::getName)
                        .orElse(null))
                .sensors(positionExt.getSensors().stream()
                        .map(SensorEcl::toEcl)
                        .collect(Collectors.toList()))
                .sensorNames(Optional.ofNullable(positionExt.getSensors())
                        .map(list -> list.stream()
                                .map(SensorExt::getName)
                                .collect(Collectors.toList()))
                        .orElse(null))
                .sensorIds(positionExt.getSensorIds())
                .build();
    }
}
